package fr.insee.solr.service.state;

import java.lang.reflect.Field;

import org.apache.commons.beanutils.NestedNullException;

import fr.insee.solr.model.FieldStrategy;
import fr.insee.solr.model.annotation.SolrField;
import fr.insee.solr.service.impl.SolrInseeException;

public class StateNestedStrategy implements IState{

	@Override
	public Object getValue(Field f, SolrField a, Object o) throws SolrInseeException {
		try {
			return a.fieldStrategy().newInstance().getValue(a.beanName(), o);
		} catch (InstantiationException | IllegalAccessException | SolrInseeException e) {
			throw new SolrInseeException(ERROR_NO_SUCH_FIELD+a.fieldName(),e);
		} catch (NestedNullException e){
			return null;
		}
	}

	@Override
	public void fillValue(Object bean, Field f, SolrField a, Object solrValue) throws SolrInseeException {
		try {
			FieldStrategy s = a.fieldStrategy().newInstance();
			s.fillValue(a.beanName(), bean, solrValue.toString());
		} catch (InstantiationException | IllegalAccessException e) {
			throw new SolrInseeException(ERROR_FILL_FIELD + f.getName(),e);
		}
		
	}

}
